package data;

import java.util.HashSet;
import java.util.Set;

import exceptions.IdentifierException;
import exceptions.DataException;


/**
 * A simple immutable class to hold a player.
 *
 * @author poet
 */
public class Player {
	private String playerName;
	private int playerID;
	public Player (String playerName, int playerID) throws DataException, IdentifierException{
		addIdentifier(playerID);
		this.playerID = playerID;
		if (playerName==null ||  playerName.trim().isEmpty())
			throw new DataException("Cannot construct Player with no name", Player.class.getName());
		this.playerName = playerName;
	}
	//Unique identifier enforcer
	private static Set<Integer> identifiers;
	private static void addIdentifier(int identifier) throws IdentifierException{
		if (Player.identifiers==null)
			Player.identifiers = new HashSet<Integer>();
		if (identifiers.contains(identifier))
			throw new IdentifierException(identifier,Game.class.getName());
		identifiers.add(identifier);
	}
	/**
	 * String immutable so no need for defensive copying.
	 */
	public String getPlayerName() {
		return playerName;
	}
	public int getPlayerID() {
		return playerID;
	}
	@Override
	public String toString(){
		return playerName;
	}
	@Override
	public int hashCode(){
		return playerID;
	}
	@Override
	public boolean equals(Object o){
		if (!(o instanceof Player))
			return false;
		if (this.playerID != ((Player)o).playerID)
			return false;
		return true;
	}
}
